package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.Menu;
import com.fayuan.nocv.entity.Role;
import com.fayuan.nocv.service.MenuService;
import com.fayuan.nocv.service.RoleService;
import com.fayuan.nocv.utils.TreeNode;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/loadAllRole")
    @ResponseBody
    public DataView loadAllRole(RoleVo roleVo){

        IPage page = new Page(roleVo.getPage(),roleVo.getLimit());

        QueryWrapper<Role>wrapper = new QueryWrapper<>();
        if (roleVo.getName()!=null){
            wrapper.like("name",roleVo.getName());
        }if (roleVo.getRemark()!=null){
            wrapper.like("remark",roleVo.getRemark());
        }


        IPage page1 = roleService.page(page, wrapper);
        return new DataView(page1.getTotal(),page1.getRecords());
    }
    @PostMapping("/addRole")
    @ResponseBody
    public DataView addRole(Role role){

         roleService.save(role);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("添加成功");
        return dataView;
    }

    @PostMapping("/deleteRole")
    @ResponseBody
    public DataView deleteRole(Role role){

        roleService.removeById(role.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除成功");
        return dataView;
    }
    @PostMapping("/updateRole")
    @ResponseBody
    public DataView updateRole(Role role){

        roleService.updateById(role);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("修改成功");
        return dataView;
    }
    @PostMapping("/initPermissionByRoleId")
    @ResponseBody
    public DataView initPermissionByRoleId(Integer roleId){
        //把所有菜单的权限查出来
        QueryWrapper<Menu>wrapper = new QueryWrapper<>();
        List<Menu>allPermissions = menuService.list();

        //根据角色rid【roleId】查询所有菜单mid
        List<Integer>currentRolePermissions = roleService.queryAllPermissionsByRid(roleId);
        //rid mid 所有的ID，去查询菜单和角色的数据
        List<Menu>menus = null;
        //查询到 mid rid
        if (currentRolePermissions.size() > 0){
            wrapper.in("id",currentRolePermissions);
            menus = menuService.list(wrapper);
        }else {
            menus = new ArrayList<>();
        }
        //返回前台的格式，带有层级关系TREENODE
        List<TreeNode>nodes = new ArrayList<>();
        for (Menu allPermission : allPermissions){
            String checkArr = "0";
            for (Menu currentPerMission : menus){
                if (allPermission.getId().equals(currentPerMission.getId())){
                    checkArr = "1";
                    break;
                }
            }
            Boolean spread = (allPermission.getOpen() == null || allPermission.getOpen() == 1)?true:false;
            nodes.add(new TreeNode(allPermission.getId(),allPermission.getPid(),allPermission.getTitle(),spread,checkArr));
        }
        return new DataView(nodes);
    }
    @PostMapping("/saveRolePermission")
    @ResponseBody
    public DataView saveRolePermission(Integer rid,Integer[] mids){
        //1.根据rid删除之前所有mid的权限
        roleService.deleteRoleByRid(rid);
        if (mids!=null && mids.length>0){
            for (Integer mid : mids){
                roleService.saveRoleMenu(rid,mid);
            }
        }
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("菜单权限分配成功");
        return dataView;
    }
}
